package com.cqupt.scanning.system.consumer.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.scanning.system.acl.entity.AclUser;
import com.cqupt.scanning.system.acl.service.AclUserService;
import com.cqupt.scanning.system.admin.entity.DeviceFoot;
import com.cqupt.scanning.system.admin.entity.DeviceThree;
import com.cqupt.scanning.system.admin.entity.PatientFoot;
import com.cqupt.scanning.system.admin.entity.PatientThree;
import com.cqupt.scanning.system.admin.entity.dto.PatientFootDTO;
import com.cqupt.scanning.system.admin.mapper.PatientFootMapper;
import com.cqupt.scanning.system.admin.service.DeviceFootService;
import com.cqupt.scanning.system.admin.service.DeviceThreeService;
import com.cqupt.scanning.system.admin.service.PatientFootService;
import com.cqupt.scanning.system.admin.service.PatientThreeService;
import com.cqupt.scanning.system.consumer.service.ConsumerService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Data: 2021/7/12 16:01
 * @Author: 宋宝梁
 */
@Service
public class ConsumerServiceImpl  implements ConsumerService {

    private AclUserService aclUserService;
    private DeviceFootService deviceFootService;
    private DeviceThreeService deviceThreeService;
    private PatientFootService patientFootService;
    private PatientThreeService patientThreeService;

    @Autowired
    public ConsumerServiceImpl(AclUserService aclUserService, DeviceFootService deviceFootService,
                               DeviceThreeService deviceThreeService, PatientFootService patientFootService,
                               PatientThreeService patientThreeService) {
        this.aclUserService = aclUserService;
        this.deviceFootService = deviceFootService;
        this.deviceThreeService = deviceThreeService;
        this.patientFootService = patientFootService;
        this.patientThreeService = patientThreeService;
    }

    @Override
    public R getAllPatsCount() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));

        int patcounts = 0;
        if (aclUser.getType().equals("步态分析")) {
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.getId()));
            if (!StrUtil.hasEmpty(deviceFoot.getDevNum())) {
                patcounts = patientFootService.count(new QueryWrapper<PatientFoot>().eq("dev_num", deviceFoot.getDevNum()));
            }
        }

        if (aclUser.getType().equals("三维扫描")) {
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().select("dev_num").eq("user_id", aclUser.getId()));
            if (!StrUtil.hasEmpty(deviceThree.getDevNum())) {
                patcounts = patientThreeService.count(new QueryWrapper<PatientThree>().eq("dev_num", deviceThree.getDevNum()));
            }
        }
        return R.ok().data("counts", patcounts);
    }

    @Override
    public R getAllPats(String query, Integer current, Integer size) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));
        if (aclUser.getType().equals("步态分析")) {
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceFoot.getDevNum();
            Page<PatientFoot> page = new Page<>(current, size);
            IPage<PatientFoot> patFootPage = patientFootService.page(page, new QueryWrapper<PatientFoot>().eq("dev_num", devNum).like("name", query));
            List<PatientFoot> records = patFootPage.getRecords();
            List<PatientFootDTO> patientsFoots = new ArrayList<>();
            for (PatientFoot record : records) {
                PatientFootDTO patientFootDTO = new PatientFootDTO();
                BeanUtils.copyProperties(record, patientFootDTO);
                patientsFoots.add(patientFootDTO);
            }
            return R.ok().data("pats", patientsFoots).data("total", patFootPage.getTotal());
        }
        if (aclUser.getType().equals("三维扫描")) {
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceThree.getDevNum();
            Page<PatientThree> page = new Page<>(current, size);
            IPage<PatientThree> patThreePage = patientThreeService.page(page, new QueryWrapper<PatientThree>()
                    .select("id","name","sex","testTime","file_path")
                    .like("name", query).eq("dev_num", devNum));
            return R.ok().data("pats", patThreePage);
        }

        return R.error();
    }

    @Override
    public R queryAllPats(String name, String ageLower,String ageUpper, String doctor, String heightLower, String heightUpper, String measureMode, String sex, String weightLower, String weightUpper,String phone,String beginTime,String endTime,Integer current, Integer size) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));

        if (aclUser.getType().equals("步态分析")) {
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceFoot.getDevNum();
            Page<PatientFoot> page = new Page<>(current, size);
            QueryWrapper<PatientFoot> w = new QueryWrapper<>();
            if (!StrUtil.isEmpty(ageLower) && !StrUtil.isEmpty(ageUpper)){
                w.ge("age", ageLower).le("age", ageUpper);
            }
            if (!StrUtil.isEmpty(doctor)){
                w.like("doctor",doctor);
            }
            if (!StrUtil.isEmpty(heightLower) && !StrUtil.isEmpty(heightUpper)){
                w.ge("height",heightLower).le("height",heightUpper);
            }
            if (!StrUtil.isEmpty(measureMode)){
                w.like("gaitMeasureMode",measureMode);
            }
            if (!StrUtil.isEmpty(sex)){
                w.eq("sex", sex);
            }
            if (!StrUtil.isEmpty(weightLower) && !StrUtil.isEmpty(weightUpper)){
                w.ge("weight",weightLower).le("weight",weightUpper);
            }
            if(!StrUtil.isEmpty(phone)){
                w.like("phone",phone);
            }
            if(!StrUtil.isEmpty(name)) {
                w.like("name",name);
            }
            if (!StrUtil.isEmpty(beginTime) && !StrUtil.isEmpty(endTime)){
                w.ge("create_time",beginTime).le("create_time",endTime);
            }
            IPage<PatientFoot> patFootPage = patientFootService.page(page, w.eq("dev_num",devNum));
            List<PatientFoot> records = patFootPage.getRecords();
            List<PatientFootDTO> patientsFoots = new ArrayList<>();
            for (PatientFoot record : records) {
                PatientFootDTO patientFootDTO = new PatientFootDTO();
                BeanUtils.copyProperties(record, patientFootDTO);
                patientsFoots.add(patientFootDTO);
            }
            return R.ok().data("pats", patientsFoots).data("total", patFootPage.getTotal());
        }
        if (aclUser.getType().equals("三维扫描")) {
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceThree.getDevNum();
            Page<PatientThree> page = new Page<>(current, size);
            QueryWrapper<PatientThree> w = new QueryWrapper<>();
            if (!StrUtil.isEmpty(ageLower) && !StrUtil.isEmpty(ageUpper)){
                w.ge("age", ageLower).le("age", ageUpper);
            }
            if (!StrUtil.isEmpty(doctor)){
                w.like("doctor",doctor);
            }
            if (!StrUtil.isEmpty(heightLower) && !StrUtil.isEmpty(heightUpper)){
                w.ge("height",heightLower).le("height",heightUpper);
            }
            if (!StrUtil.isEmpty(measureMode)){
                w.like("gaitMeasureMode",measureMode);
            }
            if (!StrUtil.isEmpty(sex)){
                w.eq("sex", sex);
            }
            if (!StrUtil.isEmpty(weightLower) && !StrUtil.isEmpty(weightUpper)){
                w.ge("weight",weightLower).le("weight",weightUpper);
            }
            if(!StrUtil.isEmpty(phone)){
                w.like("phone",phone);
            }
            if (!StrUtil.isEmpty(beginTime) && !StrUtil.isEmpty(endTime)){
                w.ge("create_time",beginTime).le("create_time",endTime);
            }
            if(!StrUtil.isEmpty(name)) {
                w.like("name",name);
            }
            IPage<PatientThree> patThreePage = patientThreeService.page(page, w.select("id","name","sex","testTime","file_path").eq("dev_num",devNum));
            return R.ok().data("pats", patThreePage);
        }

        return R.error();
    }

//    @Override
//    public R queryAllByTime(String beginTime, String endTime,Integer current, Integer size) {
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
//        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));
//        if (aclUser.getType().equals("步态分析")) {
//            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.getId()));
//            String devNum = deviceFoot.getDevNum();
//            Page<PatientFoot> page = new Page<>(current, size);
//            IPage<PatientFoot> patFootPage = patientFootService.page(page, new QueryWrapper<PatientFoot>().eq("dev_num", devNum).ge("create_time", beginTime).le("create_time", endTime));
//            List<PatientFoot> records = patFootPage.getRecords();
//            List<PatientFootDTO> patientsFoots = new ArrayList<>();
//            for (PatientFoot record : records) {
//                PatientFootDTO patientFootDTO = new PatientFootDTO();
//                BeanUtils.copyProperties(record, patientFootDTO);
//                patientsFoots.add(patientFootDTO);
//            }
//            return R.ok().data("pats", patientsFoots).data("total", patFootPage.getTotal());
//        }
//        if (aclUser.getType().equals("三维扫描")) {
//            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().select("dev_num").eq("user_id", aclUser.getId()));
//            String devNum = deviceThree.getDevNum();
//            Page<PatientThree> page = new Page<>(current, size);
//            IPage<PatientThree> patThreePage = patientThreeService.page(page, new QueryWrapper<PatientThree>()
//                    .select("id","name","sex","testTime","file_path")
//                    .ge("create_time", beginTime).le("create_time", endTime).eq("dev_num", devNum));
//            return R.ok().data("pats", patThreePage);
//        }
//
//        return R.error();
//    }
}
